package com.hk.commons;

import com.hk.commons.util.SpringContextHolder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;

/**
 * @author Kevin
 * @date 2019-07-29 22:02
 */
@RequiredArgsConstructor
public enum Status {

    /**
     * Success status
     */
    SUCCESS("operation.success", 10200),

    /**
     * Failure status
     */
    FAILURE("operation.failure", -1),

    /**
     * Redirect status
     */
    REDIRECT("operation.redirect", 10302),

    /**
     * Bad request
     */
    BAD_REQUEST("operation.bad_request", 10400),

    /**
     * Unauthorized
     */
    UNAUTHORIZED("operation.unauthorized", 10401),

    /**
     * Forbidden
     */
    FORBIDDEN("operation.forbidden", 10403),

    /**
     * Request resource not found.
     */
    NOT_FOUND("operation.not_found", 10404),

    /**
     * Request method not allowed.
     */
    METHOD_NOT_ALLOWED("operation.method_not_allowed", 10405),

    /**
     * Server error
     */
    SERVER_ERROR("operation.server_error", 10500);

    private final String message;

    @Getter
    private final int statusCode;

    public String getMessage() {
        return SpringContextHolder.getMessage(message);
    }

    public static String getMessage(int statusCode) {
        return Arrays.stream(values())
                .filter(item -> item.statusCode == statusCode)
                .findFirst()
                .map(Status::getMessage)
                .orElse(null);
    }
}
